package com.yld.car.market;

import java.util.ArrayList;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.yld.car.application.MyApplication;
import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.domain.Car;

public class PublishCarPickTypeAndStatusActivity extends BaseActivity implements
		OnItemClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.base_list);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		TextView textView = (TextView) findViewById(R.id.title);
		allCarLists = (ListView) findViewById(R.id.list1);
		allCarLists.setOnItemClickListener(this);
		mBar = (ProgressBar) findViewById(R.id.progressBar);
		utils = NetworkProgressUtils.getInstance();
		textView.setText("选择车辆类型");
		this.showBackButton();

		new GetCarTypeTask().execute("");
	}

	private NetworkProgressUtils utils;
	private ArrayList<Car> all;
	private ProgressBar mBar;

	private class GetCarTypeTask extends
			AsyncTask<String, String, ArrayList<Car>> {

		@Override
		protected ArrayList<Car> doInBackground(String... params) {

			if (utils.getAPNType(getApplicationContext()) == -1) {

				return null;
			} else {
				mBar.setVisibility(View.VISIBLE);
				Object o = utils.getWebservicesByData(ConstantUtils.NAME_SPACE,
						ConstantUtils.ALL_METHROD_NAME.get(0),
						ConstantUtils.NAME_SPACE
								+ ConstantUtils.ALL_METHROD_NAME.get(0),
						ConstantUtils.GET_BRAND_URL, null);
				// System.out.println(o + "09090909");
				all = utils.parseJson(o.toString(), "table",
						NetworkProgressUtils.BASE_CAR_TYPE,false);

				// System.out.println("all====" + all);
				// System.out.println("o=====" + o);
				// ArrayList<Car> c = utils.parseJson(o.toString(), "table",
				// NetworkProgressUtils.SEARCH_CAR_TYPE);
				// System.out.println("all:" + c);
				return all;
			}

		}

		@Override
		protected void onPostExecute(ArrayList<Car> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			if (result == null) {

				Toast.makeText(getApplicationContext(), "网络未连接，请设置网络!",
						Toast.LENGTH_SHORT).show();
			} else {
				mBar.setVisibility(View.GONE);
				CarTypeAdapter adapter = new CarTypeAdapter(result);
				allCarLists.setAdapter(adapter);
			}

		}

	}

	private ListView allCarLists;

	private class CarTypeAdapter extends BaseAdapter {

		private ArrayList<Car> all = null;

		private CarTypeAdapter(ArrayList<Car> all) {
			this.all = all;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return all.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return all.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				convertView = LayoutInflater.from(getApplicationContext())
						.inflate(R.layout.my_item_car, null);
			}

			TextView t1 = (TextView) convertView.findViewById(R.id.itemTitle);
			t1.setTextSize(22);
			t1.setTextColor(getResources().getColor(R.color.black_light));
			t1.setText(all.get(position).getName());

			// TextView t2 = (TextView)
			// convertView.findViewById(R.id.itemContent);
			// t2.setText(all.get(position).getName());
			return convertView;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long id) {

		Car c = (Car) adapterView.getAdapter().getItem(position);

		MyApplication myApplication = this.mApp;

		myApplication.setCar(c);

		finish();

	}
}
